package com.icetec.iceintel.ui.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.icetec.R;
import com.icetec.iceintel.database.DevicesListSqlHelper;
import com.icetec.iceintel.database.GroupControlSqlHelper;
import com.icetec.iceintel.framework.IceBaseActivity;
import com.icetec.iceintel.model.Device;
import com.icetec.iceintel.ui.adapter.GroupDeviceAdapter;

import org.w3c.dom.Text;

/**
 * Created by 坤道 on 2015/5/23.
 */
public class SetGroupExtraFunActivity extends IceBaseActivity{
    public static final String MAC = "mac";
    public static final int DELETE_CODE = 0;
    public static final int BACK_CODE = 1;
    private ListView deviceLv;
    private TextView groupNameTv;
    private GroupDeviceAdapter groupDeviceAdapter;
    private DevicesListSqlHelper devicesListSqlHelper;
    private Device group = null;
    private Intent intent;
    private boolean isDelete = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setgroup_func);
        groupNameTv = (TextView)findViewById(R.id.setgroup_func_name_tv);
        deviceLv = (ListView)findViewById(R.id.setgroup_func_devicelist_lv);

        devicesListSqlHelper = new DevicesListSqlHelper();
        intent = getIntent();
        Bundle bundle = intent.getExtras();
        group = devicesListSqlHelper.getSingleDevice(bundle.getString(MAC));

        if(group.getName() != null){
            groupNameTv.setText(group.getName());
        }else{
            groupNameTv.setText(R.string.unnamed_group);
        }

        groupDeviceAdapter = new GroupDeviceAdapter(getApplicationContext(), group);
        deviceLv.setAdapter(groupDeviceAdapter);
    }

    @Override
    public void onResume() {
        isDelete = false;
        group = devicesListSqlHelper.getSingleDevice(group.getMAC());
        groupNameTv.setText(group.getName());
        groupDeviceAdapter.updateFromDb();
        super.onResume();
    }

    public void edit(View v){
        Intent intent = new Intent(SetGroupExtraFunActivity.this, AddgroupActivity.class);
        intent.putExtra(AddgroupActivity.MAC, group.getMAC());
        startActivity(intent);
    }

    public void back(View v){
        finish();
    }

    @Override
    public void finish() {
        if(isDelete){
            setResult(DELETE_CODE, intent);
        }else{
            setResult(BACK_CODE, intent);
        }
        super.finish();
    }

    public void delete(View v){
        new AlertDialog.Builder(this)
                .setTitle(R.string.delete_group)
                .setMessage(R.string.is_delete_group)
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        groupDeviceAdapter.deleteDevice();
                        devicesListSqlHelper.deledeSingleDevice(group);
                        isDelete = true;
                        finish();
                    }
                })
                .setNegativeButton(R.string.cancel, null).show();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == android.view.KeyEvent.KEYCODE_BACK) {
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


}
